#! /usr/bin/env node

var path = require("path")
var spawn = require("child_process").spawn
var os = require("os")
var fs = require("fs")

var processOptions = {}
processOptions.detached = true
processOptions.stdio = ["ignore", null, null]
processOptions.env = Object.assign({}, process.env, { "LOCAL_ONI": 1 })

var isWindows = os.platform() === "win32";

var args = process.argv.slice(2);
args.unshift(path.resolve(path.join(__dirname, "..", "lib", "main", "src", "main.js")))

// In Windows, directly call into the electron executable - otherwise we"ll end up with a floating node console
var nonWindowsElectronPath = path.join(__dirname, "..", "node_modules", ".bin", "electron")
var windowsElectronPath = path.join(__dirname, "..", "node_modules", "electron", "dist", "electron.exe")
var spawnProcess = isWindows ? windowsElectronPath : nonWindowsElectronPath

var child = spawn(spawnProcess, args, processOptions)
child.unref()

child.on("error", (err) => console.error(err))
child.on("exit", (code) => process.exit(code));

process.exit(0)
